package com.hm.jiaowuxitong.ui;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import com.hm.jiaowuxitong.service.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class newUser {
    public newUser() {
        JFrame frame=new JFrame("新用户注册");
        frame.setSize(1200, 700);
        //创建画布
        JPanel panel = new JPanel(new BorderLayout());
        //创建方向布局
        SpringLayout layout = new SpringLayout();
        panel.setLayout(layout);
        //创建文本框控件
        JLabel label1 = new JLabel("账户名称:");
        label1.setFont(new Font(" ", 3, 25));
        label1.setForeground(Color.black);
        layout.putConstraint(SpringLayout.WEST, label1, 350, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label1, 150, SpringLayout.NORTH, panel);
        //创建文本编辑框控件
        JTextField jtf1 = new JTextField(16);
        layout.putConstraint(SpringLayout.NORTH, jtf1, 153, SpringLayout.NORTH, panel);
        layout.putConstraint(SpringLayout.WEST, jtf1, 5, SpringLayout.EAST, label1);
        jtf1.setFont(new Font(" ", 2, 15));
        //创建文本框控件
        JLabel label2 = new JLabel("设置密码:");
        layout.putConstraint(SpringLayout.WEST, label2, 350, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label2, 15, SpringLayout.SOUTH, label1);
        label2.setForeground(Color.black);
        label2.setFont(new Font(" ", 3, 25));
        JLabel label3 = new JLabel("确认密码:");
        layout.putConstraint(SpringLayout.WEST, label3, 350, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label3, 15, SpringLayout.SOUTH, label2);
        label3.setForeground(Color.black);
        label3.setFont(new Font(" ", 3, 25));
        //创建文本框控件
        JLabel label4 = new JLabel("选择身份:");
        layout.putConstraint(SpringLayout.WEST, label4, 350, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label4, 15, SpringLayout.SOUTH, label3);
        label4.setForeground(Color.black);
        label4.setFont(new Font(" ", 3, 25));
        //创建登录按钮
        JButton button1 = new JButton("新用户注册");
        layout.putConstraint(SpringLayout.WEST, button1, 490, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, button1, 400, SpringLayout.NORTH, panel);
        button1.setFont(new Font(" ", 3, 25));

        //密码显示
        JPasswordField passwordField = new JPasswordField(15);
        JButton toggleButton = new JButton("👁");
        layout.putConstraint(SpringLayout.WEST, toggleButton, 5, SpringLayout.EAST, passwordField);
        layout.putConstraint(SpringLayout.NORTH, toggleButton, 205, SpringLayout.NORTH, panel);
        toggleButton.setFont(new Font(" ", 3, 13));

        layout.putConstraint(SpringLayout.WEST, passwordField, 5, SpringLayout.EAST, label2);
        layout.putConstraint(SpringLayout.NORTH, passwordField, 205, SpringLayout.NORTH, panel);
        passwordField.setFont(new Font(" ", 3, 15));

        JPasswordField passwordField1 = new JPasswordField(15);
        JButton toggleButton1 = new JButton("👁");
        layout.putConstraint(SpringLayout.WEST, toggleButton1, 5, SpringLayout.EAST, passwordField1);
        layout.putConstraint(SpringLayout.NORTH, toggleButton1, 255, SpringLayout.NORTH, panel);
        toggleButton1.setFont(new Font(" ", 3, 13));

        layout.putConstraint(SpringLayout.WEST, passwordField1, 5, SpringLayout.EAST, label3);
        layout.putConstraint(SpringLayout.NORTH, passwordField1, 255, SpringLayout.NORTH, panel);
        passwordField1.setFont(new Font(" ", 3, 15));
        // 创建下拉框并添加选项
        String[] options = {"教师", "学生", "管理员"};
        JComboBox<String> comboBox = new JComboBox<>(options);

        // 设置下拉框的位置和大小
        layout.putConstraint(SpringLayout.WEST, comboBox, 5, SpringLayout.EAST, label4);
        layout.putConstraint(SpringLayout.NORTH, comboBox, 22, SpringLayout.SOUTH, passwordField1);
        comboBox.setForeground(Color.black);
        comboBox.setPreferredSize(new Dimension(215, 27));

        toggleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (passwordField.getEchoChar() == 0) {
                    passwordField.setEchoChar('*');
                } else {
                    passwordField.setEchoChar((char) 0);
                }
            }
        });
        toggleButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (passwordField.getEchoChar() == 0) {
                    passwordField.setEchoChar('*');
                } else {
                    passwordField.setEchoChar((char) 0);
                }
            }
        });
        //美化按键。
        button1.setBorderPainted(false);//不显示按钮的边框。
        button1.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton.setBorderPainted(false);//不显示按钮的边框。
        toggleButton.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton1.setBorderPainted(false);//不显示按钮的边框。
        toggleButton1.setRolloverEnabled(true);//启用按钮悬停效果。
        //把所有控件添加至画布
        panel.add(label1);
        panel.add(jtf1);
        panel.add(label2);
        panel.add(label3);
        panel.add(label4);
        panel.add(passwordField);
        panel.add(toggleButton);
        panel.add(passwordField1);
        panel.add(toggleButton1);
        panel.add(button1);

        // 将下拉框添加到 JFrame 中
        panel.add(comboBox);


        // 创建 JFXPanel 对象，用于加载视频
        JFXPanel jfxPanel = new JFXPanel();

        // 将 JFXPanel 添加到 JPanel 中心区域
        panel.add(jfxPanel, BorderLayout.CENTER);

        // 创建媒体对象并加载视频文件
        String videoFile = "Image/0d84bc92-4f77-11ed-a561-9e59ffd5e499.mp4";
        Media media = new Media(new File(videoFile).toURI().toString());

        // 创建媒体播放器对象
        MediaPlayer mediaPlayer = new MediaPlayer(media);

        // 创建媒体视图对象并设置大小
        MediaView mediaView = new MediaView(mediaPlayer);
        mediaView.setFitWidth(1200);
        mediaView.setFitHeight(675);
        // 将媒体视图添加到 JFXPanel 中
        jfxPanel.setScene(new Scene(new BorderPane(mediaView)));
        // 监听视频播放完成事件
        mediaPlayer.setOnEndOfMedia(() -> {
            // 循环播放
            mediaPlayer.seek(mediaPlayer.getStartTime());
            mediaPlayer.play();
        });
        // 播放视频
        mediaPlayer.play();
        // 将视频组件添加到面板的中心部分
        panel.add(jfxPanel, BorderLayout.CENTER);
        //把画布添加至面板
        frame.setContentPane(panel);
        //禁止面板放大缩小
        frame.setResizable(false);
        //允许关闭整个应用
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //将面板显示
        frame.setVisible(true);
        // 设置窗口居中显示
        frame.setLocationRelativeTo(null);
        // 显示窗口
        frame.setVisible(true);
        // 确保JavaFX线程正常关闭
        Platform.setImplicitExit(false);
        //添加点击事件
        button1.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                String selectedOption = (String) comboBox.getSelectedItem();
                if (selectedOption.equals("教师")) {
                    ITeacherService service = new TeacherServiceImp();
                    List<Map<String, Object>> map = service.HgetUser(jtf1.getText());
                    if (map.size()!=0) {
                        JOptionPane.showMessageDialog(null, "账户已存在", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    }
                    else if (passwordField.getText().length() == 0 || jtf1.getText().length() == 0) {
                        JOptionPane.showMessageDialog(null, "账户或密码不能为空", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else if (passwordField1.getText().length() == 0) {
                        JOptionPane.showMessageDialog(null, "请再次确认密码", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else if (!passwordField1.getText().equals(passwordField.getText())) {
                        JOptionPane.showMessageDialog(null, "输入的密码需相同", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else {
                        //把多个数据封装成一个对象，便于传输
                        String id=jtf1.getText();
                        String key=passwordField1.getText();
                        Map<String,Object> oMap=new HashMap<>();
                        oMap.put("id",id);
                        oMap.put("key",key);
                        //把封装好的数据传递给业务逻辑层接口进行加工
                        int i=service.HaddTeacher(oMap);
                        if (i>0){
                            //学生信息已添加数据库成功
                            JOptionPane.showMessageDialog(null, "注册成功", "消息提示", JOptionPane.WARNING_MESSAGE);//消息对话框
                            //释放内存
                            frame.dispose();
                        }
                    }
                }
                else if (selectedOption.equals("学生")) {
                    IStudentService service = new StudentServiceImp();
                    List<Map<String, Object>> map = service.HgetUser(jtf1.getText());
                    if (map.size()!=0) {
                        JOptionPane.showMessageDialog(null, "账户已存在", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    }
                    else if (passwordField.getText().length() == 0 || jtf1.getText().length() == 0) {
                        JOptionPane.showMessageDialog(null, "账户或密码不能为空", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else if (passwordField1.getText().length() == 0) {
                        JOptionPane.showMessageDialog(null, "请再次确认密码", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else if (!passwordField1.getText().equals(passwordField.getText())) {
                        JOptionPane.showMessageDialog(null, "输入的密码需相同", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else {
                        //把多个数据封装成一个对象，便于传输
                        String id=jtf1.getText();
                        String key=passwordField1.getText();
                        Map<String,Object> oMap=new HashMap<>();
                        oMap.put("id",id);
                        oMap.put("key",key);
                        //把封装好的数据传递给业务逻辑层接口进行加工
                        int i=service.HaddStudent(oMap);
                        if (i>0){
                            //学生信息已添加数据库成功
                            JOptionPane.showMessageDialog(null, "注册成功", "消息提示", JOptionPane.WARNING_MESSAGE);//消息对话框
                            //释放内存
                            frame.dispose();
                        }
                    }
                }
                else if (selectedOption.equals("管理员")) {
                    IAdminService service = new AdminServiceImp();
                    List<Map<String, Object>> map = service.HgetUser(jtf1.getText());
                    if (map.size()!=0) {
                        JOptionPane.showMessageDialog(null, "账户已存在", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else if (passwordField.getText().length() == 0 || jtf1.getText().length() == 0) {
                        JOptionPane.showMessageDialog(null, "账户或密码不能为空", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else if (passwordField1.getText().length() == 0) {
                        JOptionPane.showMessageDialog(null, "请再次确认密码", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else if (!passwordField1.getText().equals(passwordField.getText())) {
                        JOptionPane.showMessageDialog(null, "输入的密码需相同", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                    } else {
                        //把多个数据封装成一个对象，便于传输
                        String id=jtf1.getText();
                        String key=passwordField1.getText();
                        Map<String,Object> oMap=new HashMap<>();
                        oMap.put("id",id);
                        oMap.put("key",key);
                        //把封装好的数据传递给业务逻辑层接口进行加工
                        int i=service.HaddAdmin(oMap);
                        if (i>0){
                            //学生信息已添加数据库成功
                            JOptionPane.showMessageDialog(null, "注册成功", "消息提示", JOptionPane.WARNING_MESSAGE);//消息对话框
                            //释放内存
                            frame.dispose();
                        }
                    }
                }
            }
            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }
}
